from django.db import models


class Employee(models.Model):
    """员工表"""

    employee_name: str = models.CharField(verbose_name="员工姓名", max_length=100)
    account: str = models.CharField(verbose_name="账号", max_length=100)
    password: str = models.CharField(verbose_name="密码", max_length=100)
    position: str = models.CharField(verbose_name="岗位", max_length=100)

    class Meta:
        # 表名
        db_table = "employee"


class Product(models.Model):
    """产品表"""

    name = models.CharField(verbose_name="品名", max_length=200)
    item_code = models.CharField(verbose_name="品号", max_length=50)
    size = models.CharField(verbose_name="规格信号", max_length=200)
    # price = models.DecimalField(verbose_name='进价', max_digits=10, decimal_places=2)
    # inventory = models.IntegerField(verbose_name='库存')
    description = models.TextField(verbose_name="产品描述", null=True)
    # image = models.ImageField(verbose_name='产品图片', upload_to='products')
    is_combination = models.BooleanField(verbose_name="是否为组合产品")
    production_status = models.CharField(verbose_name="产品当前状态", max_length=50)

    class Meta:
        # 表名
        db_table = "product"


class BasicProduct(models.Model):
    """基础产品（零件）表"""

    name = models.CharField(verbose_name="基础产品名称", max_length=100)
    description = models.CharField(verbose_name="产品描述", max_length=200)

    class Meta:
        # 表名
        db_table = "basic_product"


class CombinationBasicRel(models.Model):
    """组合产品基本产品关系表"""

    # 组合产品 id
    combination_product = models.ForeignKey(
        to="Product", to_field="id", on_delete=models.CASCADE
    )
    # 组合产品的零件 id
    basic_product = models.ForeignKey(
        to="BasicProduct", to_field="id", on_delete=models.CASCADE
    )

    class Meta:
        # 表名
        db_table = "combination_basic_rel"


class ProductionProcess(models.Model):
    """生产步骤表"""

    # 生产状态
    process_name = models.CharField(verbose_name="生产状态", max_length=100)
    previous_process = models.ForeignKey(
        verbose_name="上一个生产状态",
        to="ProductionProcess",
        to_field="id",
        on_delete=models.CASCADE,
        null=True,
    )

    class Meta:
        # 表名
        db_table = "production_process"


class IssueRecord(models.Model):
    """问题记录表"""

    product = models.ForeignKey(
        verbose_name="产品ID", to="Product", to_field="id", on_delete=models.CASCADE
    )
    # 生产记录 id
    production_record = models.ForeignKey(
        to="ProductionRecord", to_field="id", on_delete=models.CASCADE
    )
    # 问题描述
    issue_description = models.CharField(max_length=200)
    # 问题记录时间
    issue_record_time = models.DateTimeField()

    class Meta:
        # 表名
        db_table = "issue_record"


class ProductionRecord(models.Model):
    """生产记录表"""

    product = models.ForeignKey(
        verbose_name="产品ID", to="Product", to_field="id", on_delete=models.CASCADE
    )
    process = models.ForeignKey(
        verbose_name="生产步骤ID",
        to="ProductionProcess",
        to_field="id",
        on_delete=models.CASCADE,
    )
    employee = models.ForeignKey(
        verbose_name="员工ID", to="Employee", to_field="id", on_delete=models.CASCADE
    )
    start_time = models.DateTimeField(verbose_name="生产开始时间")
    end_time = models.DateTimeField(verbose_name="生产结束时间")
    is_issue = models.BooleanField(verbose_name="是否有问题记录")
    description = models.TextField(verbose_name="生产描述")
    result = models.TextField(verbose_name="生产结果")

    class Meta:
        # 表名
        db_table = "production_record"


class RepairRecord(models.Model):
    """维修记录表"""

    product = models.ForeignKey(
        verbose_name="产品ID", to="Product", to_field="id", on_delete=models.CASCADE
    )
    issue_record = models.ForeignKey(
        verbose_name="问题记录ID", to="IssueRecord", to_field="id", on_delete=models.CASCADE
    )
    employee = models.ForeignKey(
        verbose_name="维修员工ID", to="Employee", to_field="id", on_delete=models.CASCADE
    )
    description = models.TextField(verbose_name="维修描述")
    start_time = models.DateTimeField(verbose_name="维修开始时间")
    end_time = models.DateTimeField(verbose_name="维修结束时间")
    result = models.TextField(verbose_name="维修结果")

    class Meta:
        # 表名
        db_table = "repair_record"
